$(document).ready(function () {
    init();
});

/*初始化*/
function init() {
    var cglxd = getUrlParam('requestid');
    $("#flushbtn").click(function () {
        location.reload(true);
    });
    if (cglxd > 0) {
        if(getcglxinfo(cglxd)==3){//采购招标立项
            if(getbspsinfo(cglxd)==3){//标书评审
                if(getgmbsinfo(cglxd)==3) {//购买标书-投标保证金
                    if(getcgjcinfo(cglxd)==3){//采购决策
                        getzbtzinfo(cglxd);//中标通知
                    }
                }
                if(gettbbzjinfo(cglxd)==3) {//购买标书-投标保证金
                    if(getcgjcinfo(cglxd)==3){//采购决策
                        getzbtzinfo(cglxd);//中标通知
                    }
                }
            }
        }
    }
}

/*采购立项*/
function getcglxinfo(cglxd) {
    var status = getCglxWfStatus(cglxd);
    var xxcglx_workflowid = getWorkflowid("xxcglx");
    $("#cglxbtn").attr("style", "display:block;");
    if (status == '') {
        var cglximg = "../images/begin.png";
        $('#cglxbtn').addClass('disabled'); //禁用 --变灰
    } else {
        var cglximg = status < 3 ? "../images/begin.png" : "../images/ok.png";
        var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?workflowid="
            +xxcglx_workflowid+"&requestid=" + cglxd;
        $("#cglxbtn1").attr("href", url);
    }
    changeAttr();
    $("#cglx").children("div").children("img").attr("src", cglximg);
    return status;
}

/*标书评审*/
function getbspsinfo(cglxd) {
    $("#bspsbtn").attr("style", "display:block;");
    $("#initbsps").remove();
    var datas = getCglxd2Status(cglxd, 'bspsxx');
    var bspsxx_workflowid = getWorkflowid("bspsxx");
    var cglxd_fieldid = getFieldid("bspsxx", "cglxd");
    var requestid = datas.requestid;
    var status = datas.currentnodetype;
    if (status == '') {
        var bspsimg = "../images/begin.png";
        var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?iscreate=1&workflowid="
            +bspsxx_workflowid+"&"+cglxd_fieldid+"=" + cglxd;
        $("#bspsbtn1").attr("href", url);
        $('#bspsbtn2').addClass('disabled'); //禁用 --变灰
    } else {
        var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?workflowid="
            +bspsxx_workflowid+"&requestid=" + requestid;
        var bspsimg = status < 3 ? "../images/begin.png" : "../images/ok.png";
        $('#bspsbtn1').addClass('disabled'); //禁用 --变灰
        $("#bspsbtn2").attr("href", url);
    }
    changeAttr();
    $("#bsps").children("div").children("img").attr("src", bspsimg);
    return status;
}

/*购买标书*/
function getgmbsinfo(cglxd) {
    $("#gmbsbtn").attr("style", "display:block;");
    $("#initgmbs").remove();
    var datas = getCglxd2Status(cglxd, 'xxbsgm');
    var xxbsgm_workflowid = getWorkflowid("xxbsgm");
    var cglxd_fieldid = getFieldid("xxbsgm", "cglxd");
    var requestid = datas.requestid;
    var status = datas.currentnodetype;
    if (status == '') {
        var zbggimg = "../images/begin.png";
        var datas1 = getBspsData(cglxd);
        var xygmbs = datas1.xygmbs;
        if(xygmbs==0){
            var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?iscreate=1&workflowid="
                +xxbsgm_workflowid+"&"+cglxd_fieldid+"=" + cglxd;
            $("#gmbsbtn1").attr("href", url);
            $('#gmbsbtn2').addClass('disabled'); //禁用 --变灰
        }else {
            $("#gmbsbtn1").addClass('disabled'); //禁用 --变灰
            $('#gmbsbtn2').addClass('disabled'); //禁用 --变灰
            if(getcgjcinfo(cglxd)==3){//采购决策
                getzbtzinfo(cglxd);//中标通知
            }
        }
    } else if (status == 3) {
        var zbggimg = "../images/ok.png";
        var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?workflowid="
            +xxbsgm_workflowid+"&requestid=" + requestid;
        $('#gmbsbtn1').addClass('disabled'); //禁用 --变灰
        $("#gmbsbtn2").attr("href", url);
    } else {
        var zbggimg = "../images/begin.png";
        var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?workflowid="
            +xxbsgm_workflowid+"&requestid=" + requestid;
        $('#gmbsbtn1').addClass('disabled'); //禁用 --变灰
        $("#gmbsbtn2").attr("href", url);
    }
    changeAttr();
    $("#gmbs").children("div").children("img").attr("src", zbggimg);
    return status;
}

/*投标保证金*/
function gettbbzjinfo(cglxd) {
    $("#tbbzjbtn").attr("style", "display:block;");
    $("#inittbbzj").remove();
    var datas = getCglxd2Status(cglxd, 'xxtbbzjjn');
    var xxtbbzjjn_workflowid = getWorkflowid("xxtbbzjjn");
    var cglxd_xxtbbzjjn_fieldid = getFieldid("xxtbbzjjn", "cglxd");
    var requestid = datas.requestid;
    var status = datas.currentnodetype;
    if (status == '') {
        var zgysimg = "../images/begin.png";
        var datas1 = getBspsData(cglxd);
        var tbbzj = datas1.tbbzj;
        if(tbbzj==0){
            var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?iscreate=1&workflowid="
                +xxtbbzjjn_workflowid+"&"+cglxd_xxtbbzjjn_fieldid+"=" + cglxd;
            $("#tbbzjbtn1").attr("href", url);
            $("#tbbzjbtn2").addClass('disabled'); //禁用 --变灰
            $("#tbbzjbtn3").addClass('disabled'); //禁用 --变灰
            $('#tbbzjbtn4').addClass('disabled'); //禁用 --变灰
            $('#tbbzjbtn5').addClass('disabled'); //禁用 --变灰
            $('#tbbzjbtn6').addClass('disabled'); //禁用 --变灰
        }else {
            $("#tbbzjbtn1").addClass('disabled'); //禁用 --变灰
            $("#tbbzjbtn2").addClass('disabled'); //禁用 --变灰
            $("#tbbzjbtn3").addClass('disabled'); //禁用 --变灰
            $('#tbbzjbtn4').addClass('disabled'); //禁用 --变灰
            $('#tbbzjbtn5').addClass('disabled'); //禁用 --变灰
            $('#tbbzjbtn6').addClass('disabled'); //禁用 --变灰
            if(getcgjcinfo(cglxd)==3){//采购决策
                getzbtzinfo(cglxd);//中标通知
            }
        }
    } else if (status == 3) {
        var zgysimg = "../images/ok.png";
        var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?workflowid="
            +xxtbbzjjn_workflowid+"&requestid=" + requestid;
        $("#tbbzjbtn1").addClass('disabled');//禁用 --变灰
        $("#tbbzjbtn2").attr("href", url);
        var datas1 = getCglxd2Status(cglxd, 'xxtbbzjth');
        var xxtbbzjth_workflowid = getWorkflowid("xxtbbzjth");
        var cglxd_xxtbbzjth_fieldid = getFieldid("xxtbbzjth", "cglxd");
        var requestid1 = datas1.requestid;
        var status1 = datas1.currentnodetype;
        if(status1==''){
            var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?iscreate=1&workflowid="
                +xxtbbzjth_workflowid+"&"+cglxd_xxtbbzjth_fieldid+"=" + cglxd;
            $("#tbbzjbtn3").attr("href", url);
            $("#tbbzjbtn4").addClass('disabled');
        }else {
            var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?workflowid="
                +xxtbbzjth_workflowid+"&requestid=" + requestid1;
            $("#tbbzjbtn3").addClass('disabled');
            $("#tbbzjbtn4").attr("href", url);
        }
        var datas2 = getCglxd2Status(cglxd, 'xxtbbzjkc');
        var xxtbbzjkc_workflowid = getWorkflowid("xxtbbzjkc");
        var cglxd_xxtbbzjkc_fieldid = getFieldid("xxtbbzjkc", "cglxd");
        var requestid2 = datas2.requestid;
        var status2 = datas2.currentnodetype;
        if(status2==''){
            var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?iscreate=1&workflowid="
                +xxtbbzjkc_workflowid+"&"+cglxd_xxtbbzjkc_fieldid+"=" + cglxd;
            $("#tbbzjbtn5").attr("href", url);
            $('#tbbzjbtn6').addClass('disabled'); //禁用 --变灰
        }else {
            var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?workflowid="
                +xxtbbzjkc_workflowid+"&requestid=" + requestid2;
            $('#tbbzjbtn5').addClass('disabled'); //禁用 --变灰
            $("#tbbzjbtn6").attr("href", url);
        }
    } else {
        var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?workflowid="
            +xxtbbzjjn_workflowid+"&requestid=" + requestid;
        var zgysimg = "../images/begin.png";
        $('#tbbzjbtn1').addClass('disabled'); //禁用 --变灰
        $('#tbbzjbtn2').attr("href", url);
        $('#tbbzjbtn3').addClass('disabled'); //禁用 --变灰
        $('#tbbzjbtn4').addClass('disabled'); //禁用 --变灰
        $('#tbbzjbtn5').addClass('disabled'); //禁用 --变灰
        $('#tbbzjbtn6').addClass('disabled'); //禁用 --变灰

    }
    changeAttr();
    $("#tbbzj").children("div").children("img").attr("src", zgysimg);
    return status;
}

/*采购决策*/
function getcgjcinfo(cglxd) {
    $("#cgjcbtn").attr("style", "display:block;");
    $("#initcgjc").remove();
    var datas = getCglxd2Status(cglxd,'fqjcxx');
    var fqjcxx_workflowid = getWorkflowid("fqjcxx");
    var cglxd_fqjcxx_fieldid = getFieldid("fqjcxx", "cglxd");
    var status = datas.currentnodetype;
    var requestid = datas.requestid;
    if (status == '') {
        var cgjcimg = "../images/begin.png";
        var url = "/spa/workflow/static4form/index.html?#/main/workflow/req?iscreate=1&workflowid="
            +fqjcxx_workflowid+"&"+cglxd_fqjcxx_fieldid+"="+cglxd;
        $("#cgjcbtn1").attr("href", url);
        $('#cgjcbtn2').addClass('disabled'); //禁用 --变灰
        $('#cgjcbtn3').addClass('disabled'); //禁用 --变灰
        $('#cgjcbtn4').addClass('disabled'); //禁用 --变灰
    } else {
        var cgjcimg = status < 3 ? "../images/begin.png" : "../images/ok.png";
        var url1 = "/spa/workflow/static4form/index.html?#/main/workflow/req?workflowid="
            +fqjcxx_workflowid+"&requestid=" + requestid;
        var jcbgxx_workflowid = getWorkflowid("jcbgxx");
        var cglxd_jcbgxx_fieldid = getFieldid("jcbgxx", "cglxd");
        var url2 = "/spa/workflow/static4form/index.html?#/main/workflow/req?iscreate=1&workflowid="
            +jcbgxx_workflowid+"&"+cglxd_jcbgxx_fieldid+"=" + cglxd;
        var newsrc = "/spa/cube/index.html#/main/cube/search?customid=75601&cglxd=" + cglxd;
        $('#cgjcbtn1').addClass('disabled'); //禁用 --变灰
        $("#cgjcbtn2").attr("href", url1);
        $("#cgjcbtn3").attr("href", url2);
        $("#cgjcbtn4").attr("data-index", newsrc);
    }
    changeAttr();
    $("#cgjc").children("div").children("img").attr("src", cgjcimg);
    return status;
}

/*中标通知*/
function getzbtzinfo(cglxd) {
    $("#zbtzbtn").attr("style", "display:block;");
    $("#initzbtz").remove();
    var newsrc = "/spa/cube/index.html#/main/cube/search?customid=75586&cglxd=" + cglxd;
    $("#zbtzbtn1").attr("data-index", newsrc);
    changeAttr();
}

//修改提示框属性
function changeAttr() {
    $('#myModal').on('show.bs.modal', function (e) {
        $("#modalid").children('iframe').attr('src', e.relatedTarget.dataset.index);
    })
}

//获取url地址参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}

//根据采购立项单、轮次获取流程状态
function getCglxWfStatus(cglxd) {
    var status = '';
    $.ajax({
        async: false,
        url: '/api/weaOfflineWfActionApi/getCglxWfStatus',
        data: {cglxd: cglxd},
        type: "get",
        dataType: "json",
        success: function (data) {
            status = data.currentnodetype;
        }, error: function (e) {
        }
    });
    return status;
}

//采购立项单转流程cglxd
function getCglxd2Status(cglxd, table) {
    var datas = {};
    $.ajax({
        async: false,
        url: '/api/weaOfflineWfActionApi/getCglxd2Status',
        data: {cglxd: cglxd, table: table},
        type: "get",
        dataType: "json",
        success: function (data) {
            datas = data.datas;
        }, error: function (e) {
        }
    });
    return datas;
}

//根据采购立项单获取需要购买标书，投标保证金
function getBspsData(cglxd) {
    var datas = {};
    $.ajax({
        async: false,
        url: '/api/weaOfflineWfActionApi/getBspsData',
        data: {cglxd: cglxd},
        type: "get",
        dataType: "json",
        success: function (data) {
            datas = data.datas;
        }, error: function (e) {
        }
    });
    return datas;
}

//根据表名字段名获取workflowid和fieldid
function getFieldid(formtable, fieldname) {
    var fieldid = "";
    $.ajax({
        async: false,
        url: '/api/weaWorkflowActionApi/getFieldid',
        data: {formtable: formtable, fieldname: fieldname},
        type: "get",
        dataType: "json",
        success: function (data) {
            fieldid = data.fieldid;
        }, error: function (e) {
        }
    });
    return fieldid;
}

//根据表名字段名获取workflowid
function getWorkflowid(formtable) {
    var workflowid = "";
    $.ajax({
        async: false,
        url: '/api/weaWorkflowActionApi/getWorkflowid',
        data: {formtable: formtable},
        type: "get",
        dataType: "json",
        success: function (data) {
            workflowid = data.workflowid;
        }, error: function (e) {
        }
    });
    return workflowid;
}

function getRwdId2Cglx(cglxd) {

}



